package cn.com.cifi.mars.util;

import cn.com.cifi.mars.bean.vo.report.InvestmentReturnReportVo;
import cn.com.cifi.mars.bean.vo.report.VsReturnReportVo;
import cn.com.cifi.mars.constant.Constants;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.*;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

/**
 *
 * 类名称：OverallReturnPlanTemplate
 * 类描述：全盘回款计划模板
 * 创建人：jyx
 * 创建时间：2020年01月07日 上午11:20:00
 * @version 1.0.0
 */

@Slf4j
public class InvestmentReturnTemplate {
    private static final String TEMPLATE_PATH = "/template/investment_return_template.xlsx";

    private XSSFWorkbook workbook;

    private XSSFSheet sheet;

    //默认sheet索引
    private int defaultSheetIndex = 0;
    //数据开始行
    private int dataBeginRow = 2;
    //当前行标
    private int currentDataRowCursor = dataBeginRow;
    //单元格样式
    private XSSFCellStyle cellStyle;

    //数据行文字单元格样式
    private XSSFCellStyle cellStyleA1;
    //数据行百分比'-'单元格样式
    private XSSFCellStyle cellStyleA2;
    //数据行百分比单元格样式
    private XSSFCellStyle cellStyleA3;

    //文本
    private static final String STRING   = "string ";
    //百分率
    private static final String NUMNER   = "number ";

    public InvestmentReturnTemplate(String name, String type, String beType, Integer year) {
        try {
            InputStream tempInputStream = InvestmentReturnTemplate.class.getResourceAsStream(TEMPLATE_PATH);
            if (tempInputStream != null) {
                workbook = new XSSFWorkbook(tempInputStream);
                sheet = workbook.getSheetAt(defaultSheetIndex);
                setSheetDate(name);
                setHeaderLine(year);

                this.appendCell(sheet.getRow(0), 56,type);
                this.appendCell(sheet.getRow(0), 106,beType);

                cellStyleA1 = sheet.getRow(2).getCell(0).getCellStyle();
                cellStyleA2 = sheet.getRow(2).getCell(1).getCellStyle();
                cellStyleA3 = sheet.getRow(2).getCell(6).getCellStyle();
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new RuntimeException(e);
        }
    }

    //标题行赋值
    private void setHeaderLine(int year) {
        int index = 6;
        for (int k = 0; k < 3 ; k++) {
            sheet.getRow(1).getCell(index++).setCellValue(year-1 + "年及以前汇总");
            for (int i = year; i < year + 3; i++) {
                int month = 1;
                for (int j = 1; j <= 12; j++) {
                    sheet.getRow(1).getCell(index++).setCellValue(i + "年"+ month++ +"月");
                }
            }
            for (int i = year + 3; i < year + 6; i++) {
                sheet.getRow(1).getCell(index++).setCellValue(i + "年第一季度");
                sheet.getRow(1).getCell(index++).setCellValue(i + "年第二季度");
                sheet.getRow(1).getCell(index++).setCellValue(i + "年第三季度");
                sheet.getRow(1).getCell(index++).setCellValue(i + "年第四季度");
            }
            sheet.getRow(1).getCell(index++).setCellValue(year+6 + "年及以后汇总");
        }



    }

    public void setSheetDate(String name) {
        workbook.setSheetName(defaultSheetIndex, workbook.getSheetName(defaultSheetIndex).replace("Sheet1", name));
    }

    /**
     * 批量插入行
     * @param list 报表数据
     * @param sheetIndex 坐标
     */
    public synchronized void appendRows(List<InvestmentReturnReportVo> list, int sheetIndex) {
        sheet = workbook.getSheetAt(sheetIndex);
        if(CollectionUtils.isEmpty(list)){
            //删除项目行的数据
            sheet.shiftRows(3, 3, -1);
        }
        this.currentDataRowCursor = this.dataBeginRow;
        for (InvestmentReturnReportVo vo : list) {
            this.appendRow(vo);
        }
    }

    /**
     * 插入行
     * @param
     */
    public synchronized void appendRow(InvestmentReturnReportVo vo) {
        XSSFRow row = sheet.getRow(currentDataRowCursor);
        if (row == null) {
            row = sheet.createRow(currentDataRowCursor);
        }
        row.setHeightInPoints(Constants.HEIGHT_POINTS);

        int index = 0;
        //项目信息
        this.appendCell(row, index++,vo.getProjectName(),STRING);
        this.appendCell(row, index++,vo.getProjectCode(),STRING);
        this.appendCell(row, index++,vo.getRegionName(),STRING);
        this.appendCell(row, index++,vo.getCityCompanyName(),STRING);
        this.appendCell(row, index++,vo.getProProductType(),STRING);
        this.appendCell(row, index++,vo.getProProductCode(),STRING);

        VsReturnReportVo historyVo = vo.getHistory();
        List<VsReturnReportVo> vsMonthVoList = vo.getVsMonthList();
        List<VsReturnReportVo> vsQuarterVoList = vo.getVsQuarterList();
        VsReturnReportVo future = vo.getFuture();
        //差异
        this.appendCell(row, index++,historyVo.getDifference(),NUMNER);
        for (int i = 0; i < 36; i++) {
            VsReturnReportVo vsMonthVo = vsMonthVoList.get(i);
            this.appendCell(row, index++,vsMonthVo.getDifference(),NUMNER);
        }
        for (int i = 0; i < 12; i++) {
            VsReturnReportVo vsQuarterVo = vsQuarterVoList.get(i);
            this.appendCell(row, index++,vsQuarterVo.getDifference(),NUMNER);
        }
        this.appendCell(row, index++,future.getDifference(),NUMNER);
        //类型
        this.appendCell(row, index++,historyVo.getCompare(),NUMNER);
        for (int i = 0; i < 36; i++) {
            VsReturnReportVo vsMonthVo = vsMonthVoList.get(i);
            this.appendCell(row, index++,vsMonthVo.getCompare(),NUMNER);
        }
        for (int i = 0; i < 12; i++) {
            VsReturnReportVo vsQuarterVo = vsQuarterVoList.get(i);
            this.appendCell(row, index++,vsQuarterVo.getCompare(),NUMNER);
        }
        this.appendCell(row, index++,future.getCompare(),NUMNER);
        //对比类型
        this.appendCell(row, index++,historyVo.getBeCompared(),NUMNER);
        for (int i = 0; i < 36; i++) {
            VsReturnReportVo vsMonthVo = vsMonthVoList.get(i);
            this.appendCell(row, index++,vsMonthVo.getBeCompared(),NUMNER);
        }
        for (int i = 0; i < 12; i++) {
            VsReturnReportVo vsQuarterVo = vsQuarterVoList.get(i);
            this.appendCell(row, index++,vsQuarterVo.getBeCompared(),NUMNER);
        }
        this.appendCell(row, index++,future.getBeCompared(),NUMNER);
        currentDataRowCursor++;
    }

    /**
     * 插入列
     * @param row 行
     * @param index 列数
     * @param value 值
     */
    private void appendCell(XSSFRow row, int index, String value){
        XSSFCell cell = row.getCell(index);
        if (cell == null) {
            cell = row.createCell(index);
        }
        cell.setCellValue(value);
    }

    /**
     * 插入列
     * @param row 行
     * @param index 列数
     * @param value 值
     * @param type 样式类型
     */
    private void appendCell(XSSFRow row, int index, Object value, String type) {
        XSSFCell cell = row.getCell(index);
        if (cell == null) {
            cell = row.createCell(index);
        }

        switch (type){
            case STRING:
                cell.setCellStyle(cellStyleA1);
                if(value == null){
                    cell.setCellValue("");
                }else {
                    cell.setCellValue((String) value);
                }
                break;
            case NUMNER:
                if(value.equals("-")){
                    cell.setCellValue((String)value);
                    cell.setCellStyle(cellStyleA2);
                }else {
                    Double rate = Double.valueOf(value.toString())/10000;
                    cell.setCellValue(rate);
                    cell.setCellStyle(cellStyleA3);

                }
                break;
        }
    }

    /**
     * 导出
     *
     * @param outputStream
     * @throws IOException
     */
    public void write(OutputStream outputStream) throws IOException {
        workbook.write(outputStream);
    }
}
